#include "homepagewidget.h"
#include "ui_homepagewidget.h"

homePageWidget::homePageWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::homePageWidget)
{
    ui->setupUi(this);
    initKindsAndTags();
    initRefrashAndTopButton();
    initVideos();
}

void homePageWidget::initKindsAndTags()
{
    //这里后期需要维护一个数据库表，用来存储变化的分类专栏，每次初始化的时候像后端请求对应的数据
    kinds = {"历史", "美⻝", "游戏", "科技", "运动", "动物", "旅⾏", "电影"};
    kindsAndTags = {
                    {"历史", {"中国史", "世界史", "历史⼈物", "艺术", "⽂化", "奇闻"}},
                    {"美⻝", {"美⻝测评", "美⻝制作", "美⻝攻略", "美⻝记录", "探店", "⽔果", "海鲜"}},
                    {"游戏", {"游戏攻略", "单机游戏", "电⼦竞技", "⼿机游戏", "⽹络游戏", "游戏赛事","桌游棋牌"}},
                    {"科技", {"数码", "软件应⽤", "智能家居", "⼿机", "电脑", "⼈⼯智能", "基础设施"}},
                    {"运动", {"篮球", "⾜球", "乒乓球", "⽻⽑球", "健⾝", "竞技体育", "运动装备"}},
                    {"动物", {"喵星⼈", "汪星⼈", "宠物知识", "动物资讯", "野⽣动物", "动物世界", "萌宠"}},
                    {"旅⾏", {"旅游攻略", "旅⾏Vlog", "⾃驾游", "交通", "环球旅⾏", "露营", "野外⽣存"}},
                    {"电影", {"电影解说", "电影推荐", "电影剪辑", "搞笑", "吐槽", "悬疑", "经典"}}};

    QPushButton * classify = buildSelectBtn(ui->classify,"#3ECEFF","分类");
    ui->classifyLayout->addWidget(classify);
    for(auto& kind : kinds)
    {
        QPushButton* btn = buildSelectBtn(ui->classify,"#222222",kind);
        ui->classifyLayout->addWidget(btn);
        connect(btn,&QPushButton::clicked,this,[=]{homePageWidget::onKindBtnClick(btn);});
    }
    ui->classifyLayout->setSpacing(8);
    resetTags(kindsAndTags["历史"]);
}

void homePageWidget::initRefrashAndTopButton()
{
    QWidget* freshAndTop = new QWidget(this);
    freshAndTop->setObjectName("freshAndTop");
    freshAndTop->setFixedSize(42,94);
    freshAndTop->setStyleSheet("QPushButton:hover{background-color:#666666}"
                               "QPushButton{"
                               "background-color : #DDDDDD;"
                               "border-radius : 21px;"
                               "border : none;}"
                               "#freshAndTop{background-color:transparent}");
    QVBoxLayout* qvlayout = new QVBoxLayout(freshAndTop);

    QPushButton* fresh = new QPushButton(freshAndTop);
    fresh->setFixedSize(42,42);
    fresh->setStyleSheet("border-image :url(:/images/homePage/shuaxin.png);");

    QPushButton* top = new QPushButton(freshAndTop);
    top->setFixedSize(42,42);
    top->setStyleSheet("border-image : url(:/images/homePage/zhiding.png)");

    connect(fresh,&QPushButton::clicked,this,[=]{onFreshBtnClick();});
    connect(top,&QPushButton::clicked,this,[=]{onTopBtnClick();});

    qvlayout->addWidget(fresh);
    qvlayout->addWidget(top);
    qvlayout->setContentsMargins(0,0,0,0);
    qvlayout->setSpacing(10);

    freshAndTop->move(1278,618);
    freshAndTop->show();

}

void homePageWidget::initVideos()
{
    for(int i=0;i<16;i++)
    {
        videoBox* box = new videoBox();
        ui->videogridLayout->addWidget(box,i/4,i%4);
    }
}

QPushButton *homePageWidget::buildSelectBtn(QWidget *parent, const QString &color, const QString &text)
{
    QPushButton* button = new QPushButton(text,parent);
    button->setMinimumHeight(26);
    button->setFixedWidth(text.size()*16+18+18);
    button->setStyleSheet("color:"+color);
    return button;
}

void homePageWidget::resetTags(const QList<QString> &list)
{
    QPushButton* btn = buildSelectBtn(ui->labels,"#3ECEFF","标签");

    ui->labelslayout->addWidget(btn);
    for(auto&tag : list)
    {
        QPushButton* newtag = buildSelectBtn(ui->labels,"#666666",tag);
        ui->labelslayout->addWidget(newtag);

        connect(newtag,&QPushButton::clicked,this,[=]{onTagsBtnClick(newtag);});
    }
    ui->labelslayout->setSpacing(4);
}

homePageWidget::~homePageWidget()
{
    delete ui;
}

void homePageWidget::onKindBtnClick(QPushButton* clickBtn)
{
    clickBtn->setStyleSheet("background-color:#F1FDFF; color:#3ECEFF;");
    //清除其他分类样式
    QList<QPushButton*> classifyChildren = ui->classify->findChildren<QPushButton*>();
    for(auto& child :classifyChildren)
    {
        if(child!=clickBtn)
        {
            child->setStyleSheet("color:#222222;");
        }
    }

    //重新设置tags标签
    QList<QPushButton*> tagsChildren = ui->labels->findChildren<QPushButton*>();
    for(auto& tag : tagsChildren)
    {
        ui->labelslayout->removeWidget(tag);
        delete tag;
    }
    resetTags(kindsAndTags[clickBtn->text()]);
}

void homePageWidget::onTagsBtnClick(QPushButton *clickBtn)
{
    clickBtn->setStyleSheet("background-color:#F1FDFF; color:#3ECEFF;");
    //清除其他分类样式
    QList<QPushButton*> classifyChildren = ui->labels->findChildren<QPushButton*>();
    for(auto& child :classifyChildren)
    {
        if(child!=clickBtn)
        {
            child->setStyleSheet("color:#666666;");
        }
    }

    //查询 & 显示
    //...
}

void homePageWidget::onFreshBtnClick()
{

}

void homePageWidget::onTopBtnClick()
{

}
